﻿
Partial Class WebForm_ManageVersionControl_ManageVersionControl
    Inherits System.Web.UI.UserControl
#Region "事件"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLUploadType()
            Me.BindVersion()
            Me.BindDDLDivision()
            Me.BindDDLSignature()

            Me.plList.Visible = True
            Me.Version = Me.ddlVersion.SelectedValue
            Me.BindGV()
        End If
    End Sub

    Protected Sub ddlUploadType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUploadType.SelectedIndexChanged
        Me.Version = Me.ddlVersion.SelectedValue
        Me.BindGV()
    End Sub

    Protected Sub ddlVersion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlVersion.SelectedIndexChanged
        Me.Version = Me.ddlVersion.SelectedValue
        Me.BindGV()
    End Sub

    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLSignature()
    End Sub
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Me.plList.Visible = True
        Me.Version = Me.ddlVersion.SelectedValue
        Me.BindGV()
    End Sub
    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub
    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim YYMM As String = Me.Version 'DateTime.Now.Date.ToString("yyyyMM")
        Dim UserID As String = clsAuth.GetUserID()

        Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
        Dim Signature As String = ""
        Select Case e.CommandName
            Case "Lock"
                Me.SaveLockData("Locked", UserID, YYMM, Me.gv.DataKeys(row.RowIndex).Item("Signature").ToString(), "Y")
                Me.BindGV()
            Case "UnLock"
                Me.SaveLockData("Locked", UserID, YYMM, Me.gv.DataKeys(row.RowIndex).Item("Signature").ToString(), "N")
                Me.BindGV()
            Case "Generate"
                Me.SaveLockData("Generated", UserID, YYMM, Me.gv.DataKeys(row.RowIndex).Item("Signature").ToString(), "Y")
                Me.BindGV()
        End Select
    End Sub
#End Region

#Region "繫結資料"
    Protected Sub BindDDLUploadType()
        ddlUploadType.DataTextField = "Text"
        ddlUploadType.DataValueField = "Value"
        ddlUploadType.DataSource = MyObj.GetCodeMappingData("ForecastUpload", "UploadType")
        ddlUploadType.DataBind()
    End Sub

    Protected Sub BindVersion()
        Me.ddlVersion.Items.Clear()
        Me.ddlVersion.Items.Add(New ListItem(DateTime.Now.ToString("yyyy/MM"), DateTime.Now.ToString("yyyyMM")))
        Me.ddlVersion.Items.Add(New ListItem(DateTime.Now.ToString("yyyy") & "Budget", DateTime.Now.ToString("yyyy") & "B"))
        Me.ddlVersion.Items.Add(New ListItem(DateTime.Now.ToString("yyyy") & "Trend", DateTime.Now.ToString("yyyy") & "T"))
        Me.ddlVersion.Items.Add(New ListItem(DateTime.Now.AddYears(1).ToString("yyyy") & "Budget", DateTime.Now.AddYears(1).ToString("yyyy") & "B"))
        Me.ddlVersion.SelectedIndex = -1
    End Sub

    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature
        Dim stManager As New SignatureManager

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Signature"
        ddlControl.DataSource = stManager.GetAllSignatureFormatted(Me.ddlDivision.SelectedValue)
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        sql.Length = 0
        sql.AppendFormat("SELECT distinct cv.Division,'('+p.Signature+')'+SPACE(1)+p.Signature_Text ")
        sql.AppendFormat("as SignatureText,p.Signature ")
        If IsNumeric(Me.Version) Then
            sql.AppendFormat(",'1' as DisplayG ")
        Else
            'Budget&Trend 不顯示Generated
            sql.AppendFormat(",'0' as DisplayG ")
        End If
        sql.AppendFormat(",case when v.Locked='Y' then '1' else '0' end as IsUnLock ")
        sql.AppendFormat(",case when v.Locked ='Y' then '0' else '1' end as IsLock ")
        sql.AppendFormat("  FROM ChannelView  cv ")
        sql.AppendFormat("INNER JOIN ProductHierarchy p ON cv.Signature=p.Signature ")
        sql.AppendFormat("LEFT JOIN VersionControl v on cv.Signature=v.Signature ")
        sql.AppendFormat("AND v.HeaderTB='{0}' ", Me.HeaderTB)
        sql.AppendFormat("AND v.YYMM='{0}' ", Me.Version) 'DateTime.Now.Date.ToString("yyyyMM")
        sql.AppendFormat("WHERE 1=1 ")

        If ddlDivision.SelectedValue <> "" Then
            sql.AppendFormat("AND cv.Division='{0}' ", ddlDivision.SelectedValue)
        End If

        If ddlSignature.SelectedValue <> "" Then
            sql.AppendFormat("AND p.Signature='{0}' ", ddlSignature.SelectedValue)
        Else
            Dim stList As New List(Of String)
            For i As Integer = 0 To Me.ddlSignature.Items.Count - 1
                stList.Add(Me.ddlSignature.Items(i).Value.ToString())
            Next
            If stList.Count > 0 Then
                sql.AppendFormat("AND p.Signature in({0}) ", MyObj.JoinFormat(",", "'{0}'", stList))
            Else
                sql.AppendFormat("AND 1=2 ")
            End If
        End If

        sql.AppendFormat("ORDER BY cv.Division,p.Signature ")
        'MyObj.WriteLog(sql.ToString())
        gv.DataSource = dat.GetData(sql)
        gv.DataBind()

    End Sub
#End Region

#Region "方法"
    Private Sub SaveLockData(ByVal ColumName As String, ByVal UserID As String, ByVal YYMM As String, ByVal Signature As String, ByVal Lock As String)
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.AppendFormat("Insert Into VersionControl(HeaderTB,YYMM,Signature,{0},CreateBy,CreateDate) ", ColumName)
        sql.AppendFormat("Select '{0}' ", Me.HeaderTB)
        sql.AppendFormat(",'{0}' ", YYMM)
        sql.AppendFormat(",'{0}' ", Signature)
        sql.AppendFormat(",'{0}'  ", Lock)
        sql.AppendFormat(",'{0}' ", UserID)
        sql.AppendFormat(",getdate() ")
        sql.AppendFormat("where not exists (Select * From VersionControl ")
        sql.AppendFormat("Where 1=1 ")
        sql.AppendFormat("AND HeaderTB = '{0}' ", Me.HeaderTB)
        sql.AppendFormat("AND YYMM = '{0}' ", YYMM)
        sql.AppendFormat("AND Signature = '{0}' ", Signature)
        sql.AppendFormat("); ")
        sql.AppendFormat("Update VersionControl Set ")
        sql.AppendFormat("{0} = '{1}', ", ColumName, Lock)
        If ColumName = "Locked" Then
            sql.AppendFormat("Fixed='{0}', ", Lock)
        End If
        sql.AppendFormat("UpdateBy='{0}', ", UserID)
        sql.AppendFormat("UpdateDate=getdate() ")
        sql.AppendFormat("Where 1=1 ")
        sql.AppendFormat("AND HeaderTB = '{0}' ", Me.HeaderTB)
        sql.AppendFormat("AND YYMM = '{0}' ", YYMM)
        sql.AppendFormat("AND Signature = '{0}'; ", Signature)

        If ColumName = "Generated" Then
            '產生下個月版本
            sql.AppendFormat("exec BuildSFReportMonth '{0}','{1}'; ", Signature, Me.HeaderTB)
            If Me.HeaderTB <> "WF_" Then
                sql.AppendFormat("exec BuildTCReportMonth '{0}','{1}'; ", Signature, Me.HeaderTB)
            End If
        End If

        Try
            dat.AccData(sql)
            'MyObj.RecordLog(sql.ToString())
            MyObj.ShowMessage("Success!")
        Catch ex As Exception
            MyObj.WriteLog(ex.ToString())
            MyObj.ShowMessage("Failed!")
        End Try

    End Sub
#End Region

    Public ReadOnly Property HeaderTB() As String
        Get
            Select Case Me.ddlUploadType.SelectedValue
                Case "SellOut"
                    Return "SO_"
                Case "PLV"
                    Return "PLV_"
                Case "WF"
                    Return "WF_"
                Case Else
                    Return ""
            End Select
        End Get
    End Property

    Public Property Version() As String
        Get
            Return ViewState("Version")
        End Get
        Set(ByVal value As String)
            ViewState("Version") = Me.ddlVersion.SelectedValue
        End Set
    End Property
End Class
